package cn.big.bigd.biocode.controller;


import cn.big.bigd.biocode.dao.CategoryDAO;
import cn.big.bigd.biocode.dao.CreditDAO;
import cn.big.bigd.biocode.dao.ToolDAO;
import cn.big.bigd.biocode.dao.UserDAO;
import cn.big.bigd.biocode.dao.vocabulary.*;
import cn.big.bigd.biocode.model.Credit;
import cn.big.bigd.biocode.model.Tool;
import cn.big.bigd.biocode.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @ClassName: UserController
 * @Description: TODO   
 * @Author: FanWang
 * @CreateDate: 2018/1/10 下午10:06
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018/1/10
 **/

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    ToolDAO toolDAO;
    @Autowired
    UserDAO userDAO;

    @Autowired
    CreditDAO creditDAO;

    @Autowired
    TechDAO techDAO;

    @Autowired
    DataTypeDAO dataTypeDAO;
    @Autowired
    PlatformDAO platformDAO;
    @Autowired
    TaskTypeDAO taskTypeDAO;
    @Autowired
    ToolTypeDAO toolTypeDAO;
    @Autowired
    UserInterfaceDAO userInterfaceDAO;
    @Autowired
    CategoryDAO categoryDAO;




    @RequestMapping(value = {"tools"})
    public String tools(Model model, Integer id, HttpServletRequest request, HttpServletResponse response) {

        HttpSession session = request.getSession();
        User user =(User) session.getAttribute("currentUser");
        System.out.println(user.getEmail());
        if(null==user){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        }
        model.addAttribute("tools",toolDAO.findByUser(user));


        return "user/tools";
    }


    @RequestMapping(value = "emails")
    @ResponseBody
    public List<User> emails(ModelMap modelMap,  String q) {

        List<User> users =userDAO.findByEmailContains(q);

        return users;
    }

    @RequestMapping(value = "{id}")
    public String id(Model model, @PathVariable Long id) {
        User  user =userDAO.findOne(id);
        List<Tool> tools =toolDAO.findByUser(user);
        List<Credit> credits = creditDAO.findDistinctToolByUser(user.getId());

//        System.out.println(credits.toString());
        Collections.sort(credits, new Comparator<Credit>() {
            @Override
            public int compare(Credit o1, Credit o2) {
                return o1.getTool().getName().compareToIgnoreCase(o2.getTool().getName());
            }
        });
//        credits.sort((Credit o1, Credit o2) -> o1.getTool().getName().compareTo(o2.getTool().getName());
//        System.out.println(credits.toString());
        model.addAttribute("tools",tools);
        model.addAttribute("user",user);
        model.addAttribute("credits",credits);
        return "user/show";
    }
}
